<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<$MTPublishCharset$>" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta name="generator" content="http://www.movabletype.org/" />
<title>Search Results</title>
<link rel="stylesheet" href="<$MTBlogURL$>styles-site.css" type="text/css" />
</head>

<body>

<div id="container">

<div id="banner">
<h1><a href="<$MTBlogURL$>" accesskey="1"><$MTBlogName$></a></h1>
<h2><$MTBlogDescription$></h2>
</div>

<div class="content">

<?php
define('CHARSET', '<$MTPublishCharset$>');
define('GOOGLE_SEARCH_WSDL', '<$MTCGIPath$>lib/MT/GoogleSearch.wsdl');
#define('GOOGLE_SEARCH_KEY', 'Your Google Web APIs license key');

function googleSearch($query, $start = 0, $maxResults = 10, $filter = false, $restrict = 'lang_ja', $safeSearch = false, $lr = 'lang_ja') {
  require_once('nusoap.php');

  $client = new soapclient(GOOGLE_SEARCH_WSDL, true);
  $client->soap_defencoding = 'utf-8';
  $client->decode_utf8 = false;

  $query = mb_convert_encoding($query, 'utf-8', CHARSET);
  $params = array('key' => GOOGLE_SEARCH_KEY,
                  'q' => $query,
                  'start' => $start,
                  'maxResults' => $maxResults,
                  'filter' => $filter,
                  'restrict' => $restrict,
                  'safeSearch' => $safeSearch,
                  'lr' => $lr,
                  'ie' => 'utf8',
                  'oe' => 'utf8'
                  );
  return $client->call('doGoogleSearch', $params);
}

$site = '<$MTBlogHost>';
$search = isset($_GET['search']) ? htmlspecialchars(trim($_GET['search'])) : "";

echo <<<EOD
<form method="get" action="{$_SERVER['PHP_SELF']}">

<h3>サイト内の検索</h3>

<p><input type="text" size="30" name="search" value="{$search}" /> <input type="submit" value="Search" /></p>

</form>


EOD;

if ($search) {
  ## Google Search
  if (defined('GOOGLE_SEARCH_KEY')) {    
    $results = googleSearch("$search site:$site");

    echo "<h2>Googleのサイト内検索結果:</h2>\n\n";

    if (is_array($results['resultElements'])) {
      $cnt = $results['estimatedTotalResultsCount'];
      echo ($cnt <= 10) ?
        "<p>{$cnt}件見つかりました。</p>\n\n" :
        "<p>約{$cnt}件見つかりました。上位10件を表示します。</p>\n\n";

      echo "<ul>\n";
      foreach ($results['resultElements'] as $result) {
        $title = $result['title'];
        $url = htmlspecialchars($result['URL']);
        $snippet = str_replace('<br>', '<br />', $result['snippet']);
        $snippet = $snippet ? $snippet : 'snippetが見つかりませんでした。';
        $string = "<li><a href=\"$url\">$title</a>\n<blockquote><div>$snippet</div></blockquote></li>\n\n";
        echo mb_convert_encoding($string, CHARSET, 'utf-8');
      }
      echo "</ul>\n\n";
    } else {
      echo "<p>見つかりませんでした。</p>\n\n";
    }
    echo "<p><small>[Powered by <a href=\"http://www.google.com/apis/\">Google Web APIs (beta)</a>]</small></p>\n\n";
  }

  ## Meta Search
  $enclist = array('utf-8', 'euc-jp', 'shift_jis');
  foreach ($enclist as $enc) {
    $qstring[$enc] = urlencode(mb_convert_encoding($search, $enc, CHARSET));
  }
  $cond_site = urlencode("site:$site");
  $searray["「{$search}」をサーチエンジンでサイト内検索:"] =
    array(
          array("MSN サーチ (ベータ)",
                "http://beta.search.msn.co.jp/results.aspx?q={$qstring['utf-8']}+$cond_site"),
          array("MSN サーチ",
                "http://search.msn.co.jp/advresults.aspx?q={$qstring['utf-8']}&amp;adv_dom={$site}"),
          array("Yahoo! Search",
                "http://search.yahoo.com/search?ei=UTF-8&amp;p={$qstring['utf-8']}+$cond_site"),
          array("Yahoo!検索",
                "http://search.yahoo.co.jp/bin/query?p={$qstring['euc-jp']}+$cond_site"),
          array("Google",
                "http://www.google.com/search?q={$qstring['utf-8']}+$cond_site")
          );
  $searray["「{$search}」をブログフィードサービスで検索:"] =
    array(
          array("Bulkfeeds",
                "http://bulkfeeds.net/app/search2?q={$qstring['utf-8']}"),
          array("FeedBack",
                "http://naoya.dyndns.org/feedback/app/search?keyword={$qstring['euc-jp']}"),
          array("未来検索 livedoor",
                "http://sf.livedoor.com/search?q={$qstring['euc-jp']}")
          );
  $searray["「{$search}」を辞書・辞典サービスで検索:"] =
    array(
          array("Yahoo!辞書 (国語)",
                "http://dic.yahoo.co.jp/bin/dsearch?p={$qstring['euc-jp']}&amp;stype=0&amp;dtype=0"),
          array("Yahoo!辞書 (英和・和英)",
                "http://dic.yahoo.co.jp/bin/dsearch?p={$qstring['euc-jp']}&amp;stype=0&amp;dtype=1"),
          array("livedoor辞書 (国語・新語)",
                "http://dic.livedoor.com/search?key={$qstring['euc-jp']}&amp;type=0&amp;kind=0"),
          array("livedoor辞書 (英和・和英)",
                "http://dic.livedoor.com/search?key={$qstring['euc-jp']}&amp;type=0&amp;kind=1"),
          array("Wikipedia日本語版",
                "http://ja.wikipedia.org/w/wiki.phtml?search={$qstring['utf-8']}&amp;fulltext=%E6%A4%9C%E7%B4%A2")
          );

  foreach ($searray as $desc => $selist) {
    echo "<h2>{$desc}</h2>\n\n<ul>\n";
    foreach ($selist as $se) {
      echo "<li><a href=\"{$se[1]}\" title=\"「{$search}」を{$se[0]}で検索\">{$se[0]}</a></li>\n";
    }
    echo "</ul>\n\n";
  }
}
?>
</div>
</div>

</body>
</html>
